from toee import *

from utilities import *

from itt import *

from math import sqrt

import _include
from co8Util.PersistentData import *

## Contained in this script


#########################################
# Persistent flags/vars/strs		#
# Uses keys starting with		#
# 'Flaggg', 'Varrr', 'Stringgg' 	#
#########################################

def get_f(flagkey):
	flagkey_stringized = 'Flaggg' + str(flagkey)
	tempp = Co8PersistentData.getData(flagkey_stringized)
	if isNone(tempp):
		return 0
	else:
		return int(tempp) != 0

def set_f(flagkey, new_value):
	flagkey_stringized = 'Flaggg' + str(flagkey)
	Co8PersistentData.setData(flagkey_stringized, new_value)

def get_v(varkey):
	varkey_stringized = 'Varrr' + str(varkey)
	tempp = Co8PersistentData.getData(varkey_stringized)
	if isNone(tempp):
		return 0
	else:
		return int(tempp)

def set_v(varkey, new_value):
	varkey_stringized = 'Varrr' + str(varkey)
	Co8PersistentData.setData(varkey_stringized, new_value)


def get_s(strkey):
	strkey_stringized = 'Stringgg' + str(strkey)
	tempp = Co8PersistentData.getData(strkey_stringized)
	if isNone(tempp):
		return ''
	else:
		return str(tempp)

def set_s(strkey, new_value):
	new_value_stringized = str(new_value)
	strkey_stringized = 'Stringgg' + str(strkey)
	Co8PersistentData.setData(strkey_stringized, new_value_stringized)


#########################################
# Bitwise NPC internal flags		#
# 					#
# Uses obj_f_npc_pad_i_3	 	#
#########################################

def npc_set(attachee,flagno):
	# flagno is assumed to be from 1 to 32
	exponent = flagno - 1
	if exponent == 31:
		abc = -1
	elif exponent > 31 or exponent < 0:
		print 'error!'
	else:
		abc = pow(2,exponent)
	tempp = attachee.obj_get_int(obj_f_npc_pad_i_3) | abc
	attachee.obj_set_int(obj_f_npc_pad_i_3, tempp)
	return	

def npc_unset(attachee,flagno):
	# flagno is assumed to be from 1 to 32
	exponent = flagno - 1
	if exponent == 31:
		abc = -1
	elif exponent > 31 or exponent < 0:
		print 'error!'
	else:
		abc = pow(2,exponent)
	tempp = (attachee.obj_get_int(obj_f_npc_pad_i_3) | abc) - abc
	attachee.obj_set_int(obj_f_npc_pad_i_3, tempp)
	return	

def npc_get(attachee,flagno):
	# flagno is assumed to be from 1 to 32
	exponent = flagno - 1
	if exponent == 31:
		abc = -1
	elif exponent > 31 or exponent < 0:
		print 'error!'
	else:
		abc = pow(2,exponent)
	return attachee.obj_get_int(obj_f_npc_pad_i_3) & abc != 0



def san_heartbeat( attachee, triggerer ):
	cur_map = game.party[0].map
	game.particles('ef-minocloud', attahcee)
	return RUN_DEFAULT


def san_dying(attachee, triggerer):
	# in case the 'script bearer' dies, pass the curse to someone else
	not_found = 1
	for pc in game.party:
		if pc.stat_level_get( stat_hp_current ) > 0 and not_found == 1:
			not_found = 0
			attachee.scripts[12] = 0
			attachee.scripts[38] = 0
			pc.scripts[12] = 439
			pc.scripts[38] = 439
			return

def san_new_map( attachee, triggerer ):
	cur_map = attachee.map
	if (cur_map == 5066):
		if get_v(455) & 1 == 0:
			record_time_stamp(460)
			set_v(455, get_v(455) | 1)
			modify_temple_level_1(attachee)
		if earth_alerted() and get_v(454) & 1 == 0:
			set_v(454, get_v(454) | 1)
			earth_reg()
	elif (cur_map == 5067):	## Temple Level 2
		if get_v(455) & 2 == 0:
			record_time_stamp(461)
			set_v(455, get_v(455) | 2)
			modify_temple_level_2(attachee)
		if water_alerted() and get_v(454) & 2 == 0:
			set_v(454, get_v(454) | 2)
			water_reg()
		if air_alerted() and get_v(454) & 4 == 0:
			set_v(454 , get_v(454) | 4)
			air_reg()
		if fire_alerted() and get_v(454) & 8 == 0:
			set_v(454, get_v(454) | 8)
			fire_reg()
	elif (cur_map == 5140):	## Temple Level 3 Lower (Thrommel, Scorpp, etc.)
		if get_v(455) & 4 == 0:
			record_time_stamp(462)
			set_v(455, get_v(455) | 4)
	elif (cur_map == 5080): ## Temple Level 4
		if get_v(455) & 8 == 0:
			record_time_stamp(463)
			set_v(455, get_v(455) | 8)
	return RUN_DEFAULT

def modify_temple_level_1(pc):

	#gnolls near southern entrance
	for gnollol in vlistxyr(558, 600, 14080, 25):
		gnollol.scripts[12] = 441
		#gnollol.destroy()
	for gnollol in vlistxyr(558, 600, 14079, 25):
		gnollol.scripts[12] = 441
		#gnollol.destroy()
	for gnollol in vlistxyr(558, 600, 14078, 25):
		gnollol.scripts[12] = 441
		#gnollol.destroy()

	# Rats
	for vaporrat in vlistxyr(497, 573, 14068, 30):
		vaporrat.scripts[12] = 441
		#vaporrat.destroy()
	for direrat in vlistxyr(440, 571, 14056, 15):
		direrat.scripts[12] = 441
		#direrat.destroy()
	for direrat in vlistxyr(534, 389, 14056, 15):
		direrat.scripts[12] = 441
		#direrat.destroy()


	#undead near secret staircase
	for skellygnoll in vlistxyr(462, 520, 14083, 100):
		skellygnoll.scripts[12] = 441
		#skellygnoll.destroy()
	for skellygnoll in vlistxyr(462, 520, 14082, 100):
		skellygnoll.scripts[12] = 441
		#skellygnoll.destroy()
	for skellygnoll in vlistxyr(462, 520, 14081, 100):
		skellygnoll.scripts[12] = 441
		#skellygnoll.destroy()
	for skellybone in vlistxyr(496, 515, 14107, 100):
		skellybone.scripts[12] = 441
		#skellybone.destroy()


	#Gnoll Leader area
	for gnoll_leader in vlistxyr(509, 534, 14066, 100):
		gnoll_leader.scripts[12] = 442
		#gnoll_leader.destroy()
	for gnoll in vlistxyr(518, 531, 14067, 66):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for gnoll in vlistxyr(518, 531, 14078, 66):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for gnoll in vlistxyr(518, 531, 14079, 66):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for gnoll in vlistxyr(518, 531, 14080, 66):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for gnoll in vlistxyr(511, 549, 14079, 33):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for gnoll in vlistxyr(511, 549, 14080, 33):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for ogre in vlistxyr(508, 536, 14249, 35):
		ogre.scripts[12] = 442
		#ogre.destroy()
	for bugbear in vlistxyr(508, 536, 14164, 35):
		bugbear.scripts[12] = 442
		#bugbear.destroy()


	#Earth critters near Ogre Chief
	for gnoll in vlistxyr(445, 538, 14078, 50):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for gnoll in vlistxyr(445, 538, 14079, 50):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for gnoll in vlistxyr(445, 538, 14080, 50):
		gnoll.scripts[12] = 442
		#gnoll.destroy()
	for ogrechief in vlistxyr(467, 535, 14248, 50):
		ogrechief.scripts[12] = 444
		#ogrechief.destroy()
	for hobgoblin in vlistxyr(467, 535, 14188, 50):
		hobgoblin.scripts[12] = 442
		#hobgoblin.destroy()
	for goblin in vlistxyr(467, 535, 14184, 27):
		goblin.scripts[12] = 442
		#goblin.destroy()
	for goblin in vlistxyr(467, 535, 14186, 27):
		goblin.scripts[12] = 442
		#goblin.destroy()
	for bugbear in vlistxyr(467, 535, 14164, 27):
		bugbear.scripts[12] = 442
		#bugbear.destroy()


	#Temple Troops near Ogre Chief
	for troop in vlistxyr(440, 500, 14337, 30):
		troop.scripts[12] = 443
		#troop.destroy()
	for fighter in vlistxyr(440, 500, 14338, 30):
		fighter.scripts[12] = 443
		#fighter.destroy()


	#ghouls and ghasts near prisoners (Morgan etc.)
	for ghast in vlistxyr(545, 535, 14137, 50):
		ghast.scripts[12] = 441
		#ghast.destroy()
	for ghast in vlistxyr(550, 545, 14136, 50):
		ghast.scripts[12] = 441
		#ghast.destroy()
	for ghast in vlistxyr(545, 553, 14135, 50):
		ghast.scripts[12] = 441
		#ghast.destroy()
	for ghoul in vlistxyr(549, 554, 14095, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()
	for ghoul in vlistxyr(549, 554, 14128, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()
	for ghoul in vlistxyr(549, 554, 14129, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()


	#harpy area
	for harpy in vlistxyr(406, 564, 14243, 100):
		harpy.scripts[12] = 441
		#harpy.destroy()
	for harpy in vlistxyr(407, 545, 14243, 100):
		harpy.scripts[12] = 441
		#harpy.destroy()
	for ghast in vlistxyr(423, 541, 14135, 50):
		ghast.scripts[12] = 441
		#ghast.destroy()
	for ghast in vlistxyr(420, 547, 14136, 50):
		ghast.scripts[12] = 441
		#ghast.destroy()
	for ghoul in vlistxyr(413, 566, 14129, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()
	for ghoul in vlistxyr(413, 566, 14128, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()
	for ghoul in vlistxyr(413, 566, 14095, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()
	for ghoul in vlistxyr(410, 526, 14129, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()
	for ghoul in vlistxyr(410, 526, 14128, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()
	for ghoul in vlistxyr(410, 526, 14095, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()


	# Gray Ooze and Gelatinous Cube
	for gelatinouscube in vlistxyr(415, 599, 14139, 100):
		gelatinouscube.scripts[12] = 441
		#gelatinouscube.destroy()
	for grayooze in vlistxyr(415, 599, 14140, 100):
		grayooze.scripts[12] = 441
		#grayooze.destroy()


	#spiders near wonnilon hideout
	for spider in vlistxyr(438, 398, 14417, 50):
		spider.scripts[12] = 441
		#spider.destroy()

	#ghouls near wonnilon hideout
	for ghoul in vlistxyr(387, 398, 14128, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()

	#ghouls near northern entrance
	for ghoul in vlistxyr(459, 600, 14129, 100):
		ghoul.scripts[12] = 441
		#ghoul.destroy()

	#ogre near southern entrance
	for ogre in vlistxyr(511, 601, 14448, 100):
		ogre.scripts[12] = 441
		#ogre.destroy()


	#Temple Troop and bugbear doormen near Earth Commander
	for troop in vlistxyr(470, 483, 14337, 25):
		troop.scripts[12] = 443
		#troop.destroy()
	for bugbear in vlistxyr(470, 483, 14165, 25):
		bugbear.scripts[12] = 442
		#bugbear.destroy()

	
	#Temple Troops and bugbears near Earth Commander
	for earthcommander in vlistxyr(450, 470, 14156, 35):
		earthcommander.scripts[12] = 444
		#earthcommander.destroy()
	for lieutenant in vlistxyr(450, 470, 14339, 35):
		lieutenant.scripts[12] = 443
		#lieutenant.destroy()
	for troop in vlistxyr(450, 470, 14337, 35):
		troop.scripts[12] = 443
		#troop.destroy()
	for bugbear in vlistxyr(450, 470, 14165, 35):
		bugbear.scripts[12] = 442
		#bugbear.destroy()


	#Earth Altar
	for worshippers in vlistxyr(482, 392, 14337, 50):
		worshippers.scripts[12] = 443
		#worshippers.destroy()
	for earthelemental in vlistxyr(482, 392, 14381, 50):
		earthelemental.scripts[12] = 442
		#earthelemental.destroy()
	for largeearthelemental in vlistxyr(483, 420, 14296, 50):
		largeearthelemental.scripts[12] = 442
		#largeearthelemental.destroy()


	#Romag, Hartsch and their bugbear guards
	#for romag in vlistxyr(445, 445, 8045, 11):
	#	romag.scripts[12] = 444
	#	romag.destroy()
	for hartsch in vlistxyr(445, 445, 14154, 11):
		hartsch.scripts[12] = 444
		#hartsch.destroy()
	for bugbear in vlistxyr(445, 445, 14162, 11):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(445, 445, 14163, 11):
		bugbear.scripts[12] = 442
		#bugbear.destroy()


	# Bugbears north of Romag
	for bugbear in vlistxyr(427, 435, 14162, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(427, 435, 14164, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(427, 435, 14165, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(418, 443, 14163, 5):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(435, 427, 14163, 5):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(435, 427, 14164, 5):
		bugbear.scripts[12] = 442
		#bugbear.destroy()


	# Bugbear "Checkpoint"
	for bugbear in vlistxyr(504, 477, 14164, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(504, 477, 14162, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(504, 477, 14163, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(504, 477, 14165, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()


	# Bugbear "reservists"
	for bugbear in vlistxyr(524, 416, 14164, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(524, 416, 14163, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for bugbear in vlistxyr(524, 416, 14162, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()



	# Wonnilon area
	for zombie in vlistxyr(546, 418, 14092, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for zombie in vlistxyr(546, 418, 14123, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for zombie in vlistxyr(546, 418, 14127, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for bugbear in vlistxyr(546, 418, 14164, 35):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for zombie in vlistxyr(546, 435, 14092, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for zombie in vlistxyr(546, 435, 14124, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for zombie in vlistxyr(546, 435, 14125, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for zombie in vlistxyr(546, 435, 14126, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for zombie in vlistxyr(546, 435, 14127, 100):
		zombie.scripts[12] = 441
		#zombie.destroy()
	for bugbear in vlistxyr(546, 435, 14164, 35):
		bugbear.scripts[12] = 442
		#bugbear.destroy()


	# Turnkey
	for bugbear in vlistxyr(570, 460, 14165, 15):
		bugbear.scripts[12] = 442
		#bugbear.destroy()
	for turnkey in vlistxyr(570, 460, 14229, 15):
		turnkey.scripts[12] = 443
		#turnkey.destroy()


	# Ogre and Goblins
	for goblin in vlistxyr(563, 501, 14186, 50):
		goblin.scripts[12] = 441
		#goblin.destroy()
	for goblin in vlistxyr(563, 501, 14187, 50):
		goblin.scripts[12] = 441
		#goblin.destroy()
	for goblin in vlistxyr(563, 501, 14185, 50):
		goblin.scripts[12] = 441
		#goblin.destroy()
	for ogre in vlistxyr(563, 501, 14448, 50):
		ogre.scripts[12] = 441
		#ogre.destroy()


	# Stirges
	for stirge in vlistxyr(410, 491, 14182, 50):
		stirge.scripts[12] = 441
		#stirge.destroy()

	return

def modify_temple_level_2(pc):
	dummy = 1
	return

#104 - romag dead
#105 - belsornig dead
#106 - kelno dead
#107 - alrrem dead
def earth_alerted():
	if game.global_flags[104] == 1: ##romag is dead
		return 0
	if tpsts(512, 1*60*60) == 1:
	# an hour has passed since you defiled the Earth Altar
		return 1
	if tpsts(507, 1) == 1:
	# You've killed the Troop Commander
		return 1
	if (tpsts(509, 1) == 1 and tpsts(504, 48*60*60) == 1) or tpsts(509, 2*60*60):
	# Either two hours have passed since you passed critter deathcount threshold, or 48 hours have passed since you first killed a critter and you've passed the threshold
	# The second condition is for the case you've killed a critter, left to rest somewhere, and returned later, and at some point crossed the kill count threshold
		return 1
	if (tpsts(510, 1) == 1 and tpsts(505, 24*60*60) == 1) or tpsts(510, 2*60*60):
	# Either two hours have passed since you passed critter deathcount threshold, or 24 hours have passed since you first killed a critter and you've passed the threshold
		return 1
	if (tpsts(511, 1) == 1 and tpsts(505, 12*60*60) == 1) or tpsts(511, 2*60*60):
	# Either 1 hour has passed since you passed troop deathcount threshold, or 12 hours have passed since you first killed a critter and you've passed the threshold
		return 1
	if tsc(457, 470) or tsc(458, 470) or tsc(459, 470): ##killed Belsornig, Kelno or Alrrem before completing 2nd earth quest
		return 1
	return 0

def water_alerted():
	if game.global_flags[105] == 1: 
	##belsornig is dead
		return 0
	if tsc(456,475) == 1 or tsc(458, 475) == 1 or tsc(459, 475) == 1: ##killed Romag, Kelno or Alrrem before accepting second water quest 
		return 1
	return 0

def air_alerted():
	if game.global_flags[106] == 1: 
		##kelno is dead
		return 0
	if tsc(456,483) or tsc(457, 483) or tsc(459, 483):
		##any of the other faction leaders are dead, and he hasn't yet given you that quest
		##Kelno doesn't take any chances
		return 1
	return 0

def fire_alerted():
	if game.global_flags[107] == 1: ##alrrem is dead
		return 0
	if (game.global_flags[104] == 1 or game.global_flags[105] == 1 or game.global_flags[106] == 1):
		# For now - if one of the other Leaders is dead
		return 1
	return 0

def is_follower(name):
	for obj in game.party:
		if (obj.name == name):
			return 1
	return 0




def destroy_weapons(npc, item1, item2, item3):
	if (item1 != 0):
		moshe = npc.item_find(item1)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	if (item2 != 0):
		moshe = npc.item_find(item2)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	if (item3 != 0):
		moshe = npc.item_find(item3)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	return


def float_comment(attachee, line):
	attachee.float_line(line,game.leader)
	return

def proactivity(npc,line_no):
	npc.turn_towards(game.party[0])
	if (critter_is_unconscious(game.party[0]) != 1 and game.party[0].type == obj_t_pc and game.party[0].d20_query(Q_Prone) == 0 and npc.can_see(game.party[0])):
		game.party[0].begin_dialog(npc,line_no)
	else:
		for pc in game.party:
			npc.turn_towards(pc)
			if (critter_is_unconscious(pc) != 1 and pc.type == obj_t_pc and pc.d20_query(Q_Prone) == 0 and npc.can_see(pc)):
				pc.begin_dialog(npc,line_no)
	return


def tsc( var1, var2 ):
#time stamp compare
#check if event associated with var1 happened before var2
#if they happened in the same second, well... only so much I can do
	if (get_v(var1) == 0):
		return 0
	elif (get_v(var2) == 0):
		return 1
	elif (get_v(var1) < get_v(var2)):
		return 1
	else:
		return 0

def tpsts(time_var, time_elapsed):
# Has the time elapsed since [time stamp] greater than the specified amount?
	if get_v(time_var) == 0:
		return 0
	if game.time.time_game_in_seconds(game.time) > get_v(time_var) + time_elapsed:
		return 1
	return 0

def record_time_stamp(tvar):
	if get_v(str(tvar)) == 0:
		set_v(str(tvar), game.time.time_game_in_seconds(game.time) )
	return


def pop_up_box(message_id):
	# generates popup box ala tutorial (without messing with the tutorial entries...)
	a = game.obj_create(11001, game.leader.location)
	a.obj_set_int(obj_f_written_text_start_line,message_id)
	game.written_ui_show(a)
	a.destroy()
	return

def within_rect_by_corners(obj, ulx, uly, brx, bry):
    xx, yy = location_to_axis(obj.location)
    if ( (xx - yy) <= (ulx-uly)) and ( (xx - yy) >= (brx-bry) ) and ( (xx + yy) >= (ulx + uly) ) and ( (xx+yy) <= (brx+bry) ):
        return 1
    return 0

def paladin_fall():
	for pc in game.party:
		pc.condition_add('fallen_paladin')

def vlistxyr( xx, yy, name, radius ):
	greg = []
	for npc in game.obj_list_vicinity( lfa(xx,yy), OLC_NPC ):
		npc_x, npc_y = lta(npc.location)
		dist = sqrt((npc_x-xx)*(npc_x-xx) + (npc_y-yy)*(npc_y-yy))
		if (npc.name == name and dist <= radius):
			greg.append(npc)
	return greg

